Imports System.Collections.Generic
Imports System.IO
Imports TreeCreeper03

Friend Class cCSVReader
    Inherits cAbstractReader

    Private pFileName As String

    Private pValueListList As List(Of List(Of String))


    Public Sub New(ByVal FileName As String)

        MyBase.New()

        Dim StreamReader As StreamReader
        Dim RowString As String
        Dim ValueList As List(Of String)

        pFileName = FileName
        pValueListList = New List(Of List(Of String))

        StreamReader = New StreamReader(FileName)
        While Not StreamReader.EndOfStream
            RowString = StreamReader.ReadLine()
            ValueList = SplitCSVRowIntoValueList(RowString)
            pValueListList.Add(ValueList)
        End While
        StreamReader.Close()

        ' number of rows
        pNumberOfRows = pValueListList.Count

        ' number of columns
        pNumberOfColumns = pValueListList.Item(0).Count

        InferMoreMetaInfoFromData()

    End Sub

    Public Overrides Function GetStringValue(ByVal RowIndexBaseOne As Integer, ByVal ColumnIndexBaseOne As Integer) As String
        Dim StringValue As String
        StringValue = pValueListList.Item(RowIndexBaseOne - 1).Item(ColumnIndexBaseOne - 1)
        Return StringValue
    End Function



    Private Function SplitCSVRowIntoValueList(ByVal RowString As String) As List(Of String)

        Dim ValueArray As String()
        Dim CountOfCommas As Integer
        Dim CountOfSemicolons As Integer
        Dim SeparatorCharacter As Char
        Dim SourceValue As String
        Dim CombinationValue As String
        Dim ValueList As List(Of String)

        CountOfCommas = Len(RowString) - Len(RowString.Replace(",", ""))
        CountOfSemicolons = Len(RowString) - Len(RowString.Replace(";", ""))

        If CountOfCommas > CountOfSemicolons Then
            SeparatorCharacter = CChar(",")
        Else
            SeparatorCharacter = CChar(";")
        End If

        ValueArray = RowString.Split(SeparatorCharacter)


        ' we want to handle properly cases like "oneA,oneB,oneX",two,three and so on
        ValueList = New List(Of String)
        CombinationValue = ""
        For Each SourceValue In ValueArray

            If CombinationValue = "" Then
                If SourceValue.IndexOf("""") < 0 Then
                    ValueList.Add(SourceValue)
                Else
                    ' begin combination
                    CombinationValue = SourceValue.Trim(CChar(""""))
                End If
            Else
                ' continue combination
                CombinationValue += SeparatorCharacter
                CombinationValue += SourceValue.Trim(CChar(""""))
                If SourceValue.IndexOf("""") > -1 Then
                    ' end combination
                    ValueList.Add(CombinationValue)
                    CombinationValue = ""
                End If
            End If

        Next

        Return ValueList

    End Function

End Class
